#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>

void Swap(int* p1, int* p2)
{
    int t = *p1;
    *p1 = *p2;
    *p2 = t;
}

SelectSort(int* arr, int len)
{

    int left = 0;
    int right = len - 1;
    int i = 0;

    while (left < right)
    {
        int maxi = left;
        int mini = left;
        for (i = left+1; i <= right; i++)
        {
            if (arr[i] > arr[maxi])
                maxi = i;
            if (arr[i] < arr[mini])
                mini = i;
        }

        Swap(arr + mini, arr + left);

        if (left == maxi)
            maxi = mini;

        Swap(arr + maxi, arr + right);

        left++;
        right--;
    }
}

int main()
{
    int arr[] = { 7,5,6,3,2,1,4,9,8 };
    int len = sizeof(arr) / sizeof(arr[0]);
    SelectSort(arr, len);

    int i = 0;
    for (i = 0; i < len; i++)
    {
        printf("%d ", arr[i]);
    }
    printf("\n");

    return 0;
}